import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute, Router } from '@angular/router';
import { Result } from '../../user/model/result';
import { UserService } from '../../user/service/user.service';

@Component({
  selector: 'app-posts',
  templateUrl: './posts.component.html',
  styleUrls: ['./posts.component.css']
})
export class PostsComponent implements OnInit {

  page: any = {
  };

  constructor(public http: HttpClient, public route: ActivatedRoute, public router: Router, public userService: UserService) { }

  ngOnInit() {
    this.http.get('/me2').subscribe(result => {
      if (result['error'] === '未登录') {
        this.userService.currentUser = null;
        // this.router.navigateByUrl('/user/login');
      } else {
        this.userService.currentUser = result['data'];
      }
    });
    this.http.get('/blogs?size=10&sort=id,desc&page=0').subscribe(result => {
      this.page = result;
    });
  }

  pageChanged(event: any) {
    const page = event.page; // 第几页，从0开始
    const size = event.rows; // 每页有几行
    this.http.get('/blogs', { params: { page: page, size: size, sort: 'id,desc' } }).subscribe(result => {
      this.page = result;
    });
  }

}
